package com.prim.background;

/**
 * 初始化还未完毕 就this赋值
 */
public class MultisThreadError4 {

    static Point point;

    public static void main(String[] args) throws InterruptedException {
        new PointMarker().start();
        //如下 根据线程时间休眠的不同导致结果不同 这也是线程的安全问题
//        Thread.sleep(10);//1 0
//        Thread.sleep(105);// 1 1
        if (point != null) {
            System.out.println(point);
        }
    }
}

class Point {
    private final int x, y;

    public Point(int x, int y) throws InterruptedException {
        this.x = x;
        MultisThreadError4.point = this;
        Thread.sleep(100);
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

class PointMarker extends Thread {
    @Override
    public void run() {
        try {
            new Point(1, 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
